package br.ucb.musicbay.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import br.ucb.musicbay.album.Album;
import br.ucb.musicbay.album.AlbumBO;
import br.ucb.musicbay.album.AlbumDataModel;
import br.ucb.musicbay.artista.Artista;
import br.ucb.musicbay.musica.MusicaDataModel;

public class AlbumManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Album album;
	private List<Album> listaAlbuns;	
	private Album[] albunsSelect;
	private AlbumDataModel albumDataModel;
	private MusicaDataModel musicaDataModel;
	private Artista artista;
	
	public AlbumManagedBean() {
		setAlbum(new Album());
		setListaAlbuns(new ArrayList<Album>());
		albumDataModel = new AlbumDataModel(getListaAlbuns());		
	}
	
	public MusicaDataModel getMusicaDataModel() {
		musicaDataModel = new MusicaDataModel(getAlbum().getMusicas());
		return musicaDataModel;
	}
	public void setMusicaDataModel(MusicaDataModel musicaDataModel) {
		this.musicaDataModel = musicaDataModel;
	}
	public List<Album> getListaAlbuns() {
		AlbumBO albumBo = new AlbumBO();
		this.listaAlbuns = albumBo.recoverAll();
		return listaAlbuns;
	}
	public void setListaAlbuns(List<Album> listaAlbuns) {
		this.listaAlbuns = listaAlbuns;
	}

	
	public Artista getArtista() {
		return artista;
	}

	public void setArtista(Artista artista) {
		this.artista = artista;
	}

	public AlbumDataModel getAlbumDataModel() {
		return albumDataModel;
	}

	public void setAlbumDataModel(AlbumDataModel albumDataModel) {
		this.albumDataModel = albumDataModel;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public Album[] getAlbunsSelect() {
		return albunsSelect;
	}

	public void setAlbunsSelect(Album[] albunsSelect) {
		this.albunsSelect = albunsSelect;
	}
	public String actionSave() {
		FacesContext context = FacesContext.getCurrentInstance();
		AlbumBO albumBO = new AlbumBO();
		if (albumBO.save(this.album)) {
			if(context.getViewRoot().getLocale().equals(new Locale("en")))
				context.addMessage(null, new FacesMessage("Album " + album.getNome() +"/"+ album.getArtista().getNome()+ " registered with success!"));
			else
				context.addMessage(null, new FacesMessage("Album " + album.getNome() +"/"+ album.getArtista().getNome()+ " cadastrado com sucesso!"));
			album = null;
			setArtista(new Artista());
			return "sucesso";
		} else {
			if(context.getViewRoot().getLocale().equals(new Locale("en")))
				context.addMessage(null, new FacesMessage("Attention! Error while registering album!"));
			else
				context.addMessage(null, new FacesMessage("Atencao! Erro ao cadastrar album!"));
			setArtista(new Artista());
			return "falha";
		}
	}

	public String actionDelete() {
		FacesContext context = FacesContext.getCurrentInstance();
		AlbumBO albumBO = new AlbumBO();
		if (albumBO.delete(getAlbunsSelect())) {
			if(context.getViewRoot().getLocale().equals(new Locale("en")))
				context.addMessage(null, new FacesMessage("Deletion was successful"));
			else
				context.addMessage(null, new FacesMessage("Exclusao realizada com sucesso!"));
			albumDataModel = new AlbumDataModel(getListaAlbuns());
			return "sucesso";
		} else {
			if(context.getViewRoot().getLocale().equals(new Locale("en")))
				context.addMessage(null, new FacesMessage("Attention! Error while deleting album!"));
			else
				context.addMessage(null, new FacesMessage("Atencao! Erro ao deletar album!"));
			return "falha";
		}
	}

	public String actionUpdate(ActionEvent event) {
		FacesContext context = FacesContext.getCurrentInstance();
		AlbumBO albumBO = new AlbumBO();
				
		byte[] by = (byte[]) event.getComponent().getAttributes().get("img");
		if (by != null) {
			getAlbum().setImg(by);
		}
		
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("fileUploadController", new FileUploadController());  		
		if (albumBO.update(this.album)) {
			if(context.getViewRoot().getLocale().equals(new Locale("en")))
				context.addMessage(null, new FacesMessage("Album " + album.getNome() +"/"+ album.getArtista().getNome()+ " Updated with success!"));
			else
				context.addMessage(null, new FacesMessage("Album " + album.getNome() +"/"+ album.getArtista().getNome()+ " atualizado com sucesso!"));
			return "sucesso";
		} else {
			if(context.getViewRoot().getLocale().equals(new Locale("en")))
				context.addMessage(null, new FacesMessage("Attention! Error while updating album!"));
			else
				context.addMessage(null, new FacesMessage("Atencao! Erro ao atualizar album!"));
			return "falha";
		}
	}
}
